// pages/Home/Home.js
var app = getApp()
let db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperImgs: [],
    openedIndex: "",
    isOpend: false,
    classify: [],
    detailList: [],
    showLoading: false,
    noMsg: "",
    imgs: [
      "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1419033806,367512616&fm=26&gp=0.jpg",
      "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1708846756,4289616053&fm=26&gp=0.jpg",
      'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2546115916,463878256&fm=26&gp=0.jpg',
      'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1597311192281&di=87f96b0382879ed414f07b3d2d67f055&imgtype=0&src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20180829%2Fe13ba00b5f9640fc8f0a07bff3142a39.jpeg'
    ]
  },
  getSwiperImgs() {
    let col = db.collection('swiperImgs')
    col.get()
      .then(res => {
        this.setData({
          swiperImgs:res.data
        })
      })
  },
  openList(e) {
    this.setData({
      detailList: []
    })
    let openedIndex = e.currentTarget.dataset.index;
    // console.log(openedIndex, this.data.openedIndex)
    if (openedIndex !== this.data.openedIndex) {
      this.setData({
        openedIndex,
        isOpened: true,
        showLoading: true,
      })
    } else {
      this.setData({
        openedIndex: "",
        isOpend: false
      })
    }
    //获取数据
    let classifyId = this.data.classify[openedIndex]._id;
    const col = db.collection('interviewDetail');
    col.where({
        classifyId
      })
      .get()
      .then(res => {
        console.log(res)
        this.setData({
          detailList: res.data,
          showLoading: false,
          noMsg: ''
        })
        if (this.data.detailList.length == 0) {
          this.setData({
            noMsg: "没有更多数据了"
          })
        }
      })
  },
  gotoDetail(e) {
    let detail = e.currentTarget.dataset.de
    app.globalData.detail = detail
    wx.navigateTo({
      url: '/pages/Detail/Detail',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSwiperImgs()
    const col = db.collection('interviewClassify');
    col.get()
      .then(res => {
        app.classify = res.data;
        this.setData({
          classify: res.data
        })
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {


  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //获取数据
    const col = db.collection('interviewClassify');
    col.get()
      .then(res => {
        app.classify = res.data;
        this.setData({
          classify: res.data
        })
        wx.stopPullDownRefresh()
      })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})